package com.nothing.controller;

import java.net.URL;
import java.util.ResourceBundle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;

import com.nothing.config.StageManager;

import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.VLineTo;
import javafx.util.Duration;

/**
 * @author shíQíang㊚
 * @since 2019年5月24日11:28:32
 */

@Controller
public class AnimaController implements Initializable {

	@Lazy
	@Autowired
	private StageManager stageManager; 

	@FXML
	private Pane aPane;
	
	@FXML
	private BorderPane  borderPane;
	
	@FXML
	private VBox faceVbox;
	
	@FXML
	private Pane facePane;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) { 
		
		// 创建按钮并设置响应
        Button but = new Button("下一张");
        but.setOnAction((ActionEvent e)->{
        	this.showNext();
        });

        // 加载图片
        for(int i=0; i<2; i++) {
            images[i] = new Image(getClass().getResourceAsStream(imageUrls[i]));
        }
        int width = 190;
        int height = 180;
        // 保持比例，适应窗口显示
        //faceOne.setPreserveRatio(true)
        faceOne.setLayoutX(5);
        faceOne.setLayoutY(690);
        faceOne.setFitWidth(width);
        faceOne.setFitHeight(height);
        faceOne.setImage(images[currentIndex]);
        
        //faceTwo.setPreserveRatio(true);
        faceTwo.setLayoutX(5);
        faceTwo.setLayoutY(690);
        faceTwo.setFitWidth(width);
        faceTwo.setFitHeight(height);
        faceTwo.setImage(images[currentIndex]);
       
        //faceThree.setPreserveRatio(true);
        faceThree.setLayoutX(5);
        faceThree.setLayoutY(690);
        faceThree.setFitWidth(width);
        faceThree.setFitHeight(height);
        faceThree.setImage(images[currentIndex]);
       
        faceFour.setLayoutX(5);
        faceFour.setLayoutY(690);
        faceFour.setFitWidth(width);
        faceFour.setFitHeight(height);
        faceFour.setImage(images[currentIndex]);
      
        faceFive.setLayoutX(5);
        faceFive.setLayoutY(690);
        faceFive.setFitWidth(width);
        faceFive.setFitHeight(height);
        faceFive.setImage(images[currentIndex]);
       
        
        //创建矩形，起点在x=100,y=50的位置 
  		facePane.getChildren().addAll(faceOne,faceTwo,faceThree,faceFour,faceFive); 
  	
  		//borderPane.getChildren().add(imv);
//  		
//  		double time = 10000; 
//  		int end = -180;
//  		final Timeline timeline=new Timeline();
//  		timeline.setCycleCount(Timeline.INDEFINITE);//设置周期运行循环往复
//  		timeline.setAutoReverse(false);//设置动画的自动往返效果 
//  		final KeyValue kv=new KeyValue(faceOne.yProperty(), end);
//  		final KeyFrame kf=new KeyFrame(Duration.millis(faceOne.getY()/timeline.getRate()), kv); 
//  		timeline.setRate(3);
//  		timeline.getKeyFrames().add(kf);
//  	  
//  		//将y的位置在1000ms内移动到300处
//  		double timeT = 12261; 
//  		final Timeline timelineT=new Timeline();
//  		timelineT.setCycleCount(Timeline.INDEFINITE);//设置周期运行循环往复
//  		timelineT.setAutoReverse(false);//设置动画的自动往返效果 
//  		final KeyValue kvT=new KeyValue(faceTwo.yProperty(), end);
//  		final KeyFrame kfT=new KeyFrame(Duration.millis(faceTwo.getY()/timelineT.getRate()),kvT); 
//  		timelineT.setRate(3);
//  		timelineT.getKeyFrames().add(kfT);
//  		 
//  		//将y的位置在1000ms内移动到300处
//  		double timeS = 14523; 
//  		final Timeline timelineS=new Timeline();
//  		timelineS.setCycleCount(Timeline.INDEFINITE);//设置周期运行循环往复
//  		timelineS.setAutoReverse(false);//设置动画的自动往返效果 
//  		final KeyValue kvS=new KeyValue(faceThree.yProperty(), end);
//  		final KeyFrame kfS=new KeyFrame(Duration.millis(faceThree.getY()/timelineS.getRate()), kvS); 
//  		timelineS.setRate(3);
//  		timelineS.getKeyFrames().add(kfS);
//  		
//  		//将y的位置在1000ms内移动到300处
//  		double timeF = 16784; 
//  		final Timeline timelineF=new Timeline();
//  		timelineF.setCycleCount(Timeline.INDEFINITE);//设置周期运行循环往复
//  		timelineF.setAutoReverse(false);//设置动画的自动往返效果 
//  		final KeyValue kvF=new KeyValue(faceFour.yProperty(), end);
//  		final KeyFrame kfF=new KeyFrame(Duration.millis(faceFour.getY()/timelineF.getRate()), kvF); 
//  		timelineF.setRate(3);
//  		timelineF.getKeyFrames().add(kfF);
//  		
//  		
//  		
//  		
//  		////////////////////////////////////////////// 
//  		//将关键帧加到时间轴中
//  		
//  		timeline.play();//运行 
//  		timelineT.play();
//  		timelineS.play();
//  		timelineF.play();
  		//创建路径
        javafx.scene.shape.Path path=new javafx.scene.shape.Path(); 
        path.getElements().add(new MoveTo(90, 0));
        path.getElements().add(new VLineTo(-800));

        //path.getElements().add(new CubicCurveTo(0, 120, 0, 240, 380, 240));
        //创建路径转变
        PathTransition pt=new PathTransition();
        //pt.setDuration(Duration.millis(4000));//设置持续时间4秒
        pt.setRate(0.1);
        pt.setPath(path);//设置路径
        pt.setNode(faceOne);//设置物体
        
        //pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
        //设置周期性，无线循环
        pt.setCycleCount(Timeline.INDEFINITE);
        pt.setAutoReverse(false);//自动往复
        pt.play();//启动动画
        
        javafx.scene.shape.Path pathT=new javafx.scene.shape.Path();
        pathT.getElements().add(new MoveTo(90,0));
        pathT.getElements().add(new VLineTo(-800));
        PathTransition ptT=new PathTransition();
        //pt.setDuration(Duration.millis(4000));//设置持续时间4秒
        ptT.setRate(0.1);
        ptT.setPath(pathT);//设置路径
        ptT.setNode(faceTwo);//设置物体
        ptT.setDelay(Duration.millis(800));
        //pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
        //设置周期性，无线循环
        ptT.setCycleCount(Timeline.INDEFINITE);
        ptT.setAutoReverse(false);//自动往复
        ptT.play();//启动动画
       
        javafx.scene.shape.Path pathS=new javafx.scene.shape.Path();
        pathS.getElements().add(new MoveTo(90,0));
        pathS.getElements().add(new VLineTo(-800));
        PathTransition ptS=new PathTransition();
        //pt.setDuration(Duration.millis(4000));//设置持续时间4秒
        ptS.setRate(0.1);
        ptS.setPath(pathS);//设置路径
        ptS.setNode(faceThree);//设置物体
        ptS.setDelay(Duration.millis(1600));
        //pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
        //设置周期性，无线循环
        ptS.setCycleCount(Timeline.INDEFINITE);
        ptS.setAutoReverse(false);//自动往复
        ptS.play();//启动动画
        
        javafx.scene.shape.Path pathF=new javafx.scene.shape.Path();
        pathF.getElements().add(new MoveTo(90,0));
        pathF.getElements().add(new VLineTo(-800));
        PathTransition ptF=new PathTransition();
        //pt.setDuration(Duration.millis(4000));//设置持续时间4秒
        ptF.setRate(0.1);
        ptF.setPath(pathF);//设置路径
        ptF.setNode(faceFour);//设置物体
        ptF.setDelay(Duration.millis(2400));
        //pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
        //设置周期性，无线循环
        ptF.setCycleCount(Timeline.INDEFINITE);
        ptF.setAutoReverse(false);//自动往复
        ptF.play();//启动动画
        
        javafx.scene.shape.Path pathI=new javafx.scene.shape.Path();
        pathI.getElements().add(new MoveTo(90,0));
        pathI.getElements().add(new VLineTo(-800));
        PathTransition ptI=new PathTransition();
        //pt.setDuration(Duration.millis(4000));//设置持续时间4秒
        ptI.setRate(0.1);
        ptI.setPath(pathI);//设置路径
        ptI.setNode(faceFive);//设置物体
        ptI.setDelay(Duration.millis(3200));
        //pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
        //设置周期性，无线循环
        ptI.setCycleCount(Timeline.INDEFINITE);
        ptI.setAutoReverse(false);//自动往复
        ptI.play();//启动动画
//  		borderPane.setTop(but); // 上面显示按钮
//        borderPane.setCenter(faceOne); // 中间显示图片


	}
	// 资源图片,在application下设置一个新的包res放3张图片，名字为hangmu.jpg   tiangong.jpg   kongjing500.jpg
    String[] imageUrls = {
        "/images/scan.gif",
        "/images/edit.png" 
    };

    Image[] images = new Image[2];
    
    
    private ImageView faceOne = new ImageView();
    private ImageView faceTwo = new ImageView();
    private ImageView faceThree = new ImageView();
    private ImageView faceFour = new ImageView();
    private ImageView faceFive = new ImageView();
    
    int currentIndex = 0;  // 当前id

 

    // 显示下一张图片
    public void showNext() {
        currentIndex++;
        if(currentIndex>1) currentIndex = 0;
        faceOne.setImage(images[currentIndex] );
    }
}
